package com.gin.kotlin.no_gay_alliance.activity

import android.content.Context
import android.content.Intent
import com.fasterxml.jackson.core.type.TypeReference
import com.gin.kotlin.no_gay_alliance.adapter.TopicListAdapter
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.databinding.ActivityBlockedTopicsBinding
import com.gin.nga.response.topic.TopicInfo
import com.gin.retrofit.utils.JacksonUtils

/**
 * 已屏蔽主题活动
 * @property topicList 主题列表
 * @property adapter TopicListAdapter
 */
class BlockedTopicsActivity : AbstractBindingActivity<ActivityBlockedTopicsBinding>() {
    private var topicList: List<TopicInfo>? = null
    private lateinit var adapter: TopicListAdapter
    override fun onCreate() {
        val b = this.b ?: return
        this.adapter = TopicListAdapter(this, handler!!, hideForumName = true)
        b.recyclerView.adapter = adapter
        adapter.replaceData(this.topicList)
    }

    override fun handleIntent(intent: Intent) {
        try {
            val text = parseArgument(intent, TopicInfo::class.java.simpleName, String::class.java)
            this.topicList = JacksonUtils.MAPPER.readValue(text, object : TypeReference<List<TopicInfo>>() {})
        } catch (e: Exception) {
            AlertDialogUtils.builder(this,"启动错误",e.localizedMessage)
                .setOnDismissListener { finish() }
                .show()
        }
    }

    override fun onResume() {
        super.onResume()
        setToolbarTitle("已屏蔽主题")
    }

    companion object {
        @JvmStatic
        fun start(context: Context, list: List<TopicInfo>) {
            if (list.isEmpty()) return
            context.startActivity(Intent(context, BlockedTopicsActivity::class.java).putExtra(TopicInfo::class.java.simpleName, JacksonUtils.MAPPER.writeValueAsString(list)))
        }
    }
}